package practice._misc

/**
  * Created by lovepocky(lovepocky@foxmail.com) on 2016/4/13.
  */
object Monad_MapAndFlatMap {
	def main(args: Array[String]) {
		/** map flatMap monad */
		println("start")
		val xs = List(1, 2, 3)
		
		val ys = List(1, 2, 3)
		def mapedF1(input: Int) = ys.map(input * _)
		
		println("x.map(mapedF1): " + xs.map(mapedF1))
		println("x.flatMap(mapedF1): " + xs.flatMap(mapedF1))
		
		//example group 1:　distinguish 'nest' and 'chain'
		//x.map(mapedF1).map(mapedF1)) //Obviously ERROR
		println("x.map(mapedF1).map(_.map(mapedF1)): " + xs.map(mapedF1).map(_.map(mapedF1)))
		println("x.map(mapedF1).map(_.flatMap(mapedF1)): " + xs.map(mapedF1).map(_.flatMap(mapedF1)))
		
		println("x.flatMap(mapedF1).map(mapedF1): " + xs.flatMap(mapedF1).map(mapedF1))
		println("x.flatMap(mapedF1).flatMap(mapedF1): " + xs.flatMap(mapedF1).flatMap(mapedF1))
		
		//example group 2
		println("for statement: " + (for (x <- xs; y <- ys) yield {
			x * y
		}) + " == x.flatMap(mapedF1)")
		
		println("end")
	}
	
}
